import 'package:library_base/entities/account_rule_entity.dart';
import 'package:library_base/entities/car_entity.dart';
import 'package:library_base/entities/driver_entity.dart';
import 'package:library_base/entities/goods_child_entity.dart';
import 'package:library_base/entities/shipper_entity.dart';
import 'package:library_base/entities/suppliers_entity.dart';
import 'package:library_base/entities/task_detail_entity.dart';
import 'package:library_base/entities/warehouse_entity.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:library_base/provider/base_controller.dart';
import 'package:get/get.dart';
import 'package:library_base/route/routers.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/date_util.dart';
import 'package:library_base/utils/get_util.dart';
import 'package:library_base/utils/toast_util.dart';

class IncomingOrdersFilterController extends BaseController {
  var loadingTime = '';

  var loginEnabled = false.obs;

  DateTime? currentTime;

  Rxn<WarehouseEntity?> warehouse = Rxn<WarehouseEntity?>();

  late Function(WarehouseEntity?,DateTime?)? filter;

  late Function()? reset;

  @override
  Future requestData() {
    currentTime = getArguments()['currentTime'];
    filter = getArguments()['filter'];
    reset = getArguments()['reset'];
    warehouse.value = getArguments()['warehouse'];
    loadingTime = DateUtil.formatDate(currentTime, format: DataFormats.zh_y_mo_d);
    loginEnabled.value = isCanSubmit();
    baseState!.setSafeState();
    return super.requestData();
  }

  chooseLoadingTime() {
    DatePicker.showDatePicker(context!,
        showTitleActions: true,
        minTime: DateTime(2021, 1, 1),
        maxTime: DateTime(2036, 1, 1),
        theme: DatePickerTheme(
          doneStyle: TextStyles.primaryColor28,
          cancelStyle: TextStyles.textNormal28,
          itemStyle: TextStyles.primaryColor36,
        ),
        currentTime: currentTime ?? DateTime.now(),
        onChanged: (date) {}, onConfirm: (date) {
      currentTime = date;
      loadingTime = DateUtil.formatDate(currentTime, format: DataFormats.zh_y_mo_d);
      loginEnabled.value = isCanSubmit();
      baseState!.setSafeState();
    }, locale: LocaleType.zh);
  }

  chooseWarehouse() {
    requestUniversal<WarehouseEntity>(Apis.WAREHOUSE, method: FBResultFul.GET,
        resultSuccessListCall: (list) async {
      var callbackSuppliersEntity = await Get.toNamed(Routers.chooseWarehousePage,
          arguments: {'Warehouse': list, 'SelectedWarehouse': warehouse.value});
      if (callbackSuppliersEntity != null && warehouse.value != callbackSuppliersEntity) {
        warehouse.value = callbackSuppliersEntity;
        loginEnabled.value = isCanSubmit();
        baseState!.setSafeState();
      }
    });
  }

  platoon() {
    if(warehouse.value == null && currentTime == null){
      ToastUtil.normal('收货仓和时间至少需要选择一个');
      return;
    }
    filter?.call(warehouse.value,currentTime);
    GetHelper.back();
  }

  resetFilter(){
    reset?.call();
    GetHelper.back();
  }

  bool isCanSubmit() {
    return true;
  }

}
